package com.zhuoyue.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.api.service.InfraredDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 此方法主要用于监听只能空调管家:即：0FAD0207设备数据上报且解析后的业务逻辑
 *
 * @author wjming
 */
@Component
@RabbitListener(queues = "${topic.rabbit.addAcCenter}")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AcCenterListener {
    private final InfraredDeviceService infraredDeviceService;

    /**
     * 此方法主要用于监听只能空调管家:即：0FAD0207设备数据上报且解析后的业务逻辑
     *
     * @param getMap
     */
    @RabbitHandler
    public void addCentralAir(Map<String, Object> getMap) {
        final String acGatewayUpdate = "acGatewayUpdate";
        final String acUpdate = "acUpdate";

        String deviceMac = (String) getMap.get("deviceMac");
        String gatewayMac = (String) getMap.get("gatewayMac");
        String type = (String) getMap.get("type");
        Object actions = getMap.get("actions");
        Map<String, Object> map = JSONObject.parseObject(JSON.toJSONString(actions));
        if (map == null) {
            return;
        }
        try {
            if (acGatewayUpdate.equals(type)) {
                infraredDeviceService.addAcCenter(deviceMac, gatewayMac, map);
            }
            if (acUpdate.equals(type)) {
                infraredDeviceService.updateAc(deviceMac, gatewayMac, map);
            }
        } catch (Exception e) {

        }

        return;
    }
}
